﻿//
// AdminViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;

namespace RSD.ViewModel
{
    using Data;
    using Messages;

    public class AdminViewModel : ViewModelBase
    {
        private readonly IItemClassificationRepository itemClassificationRepository;
        private readonly IUserRepository userRepository;
        private readonly IPositionRepository positionRepository;

        private readonly RelayCommand createItemCommand;
        private readonly RelayCommand createUserCommand;
        private readonly RelayCommand createPositionCommand;

        private ItemClassification selectedItem;
        private User selectedUser;
        private Position selectedPosition;

        public AdminViewModel(IUserRepository userRepository,
            IItemClassificationRepository itemClassificationRepository,
            IPositionRepository positionRepository)
        {
            this.userRepository = userRepository;
            this.itemClassificationRepository = itemClassificationRepository;
            this.positionRepository = positionRepository;

            this.Users = new ObservableCollection<User>(userRepository.FindAll());
            this.Items = new ObservableCollection<ItemClassification>(
                itemClassificationRepository.FindAll());
            this.Positions = new ObservableCollection<Position>(
                positionRepository.FindAll());

            this.createItemCommand = new RelayCommand(CreateItemExecute);
            this.createUserCommand = new RelayCommand(CreateUserExecute);
            this.createPositionCommand = new RelayCommand(CreatePositionExecute);

            Messenger.Default.Register<ItemClassificationCreatedMessage>(this,
                msg =>
                {
                    itemClassificationRepository.Add(msg.ItemClassification).SaveChanges();
                    Items.Add(msg.ItemClassification);
                });

            Messenger.Default.Register<UserCreatedMessage>(this,
                msg =>
                {
                    userRepository.Add(msg.User).SaveChanges();
                    Users.Add(msg.User);
                });

            Messenger.Default.Register<PositionCreatedMessage>(this,
                msg =>
                {
                    positionRepository.Add(msg.Position).SaveChanges();
                    Positions.Add(msg.Position);
                });
        }

        public User SelectedUser
        {
            get
            {
                return selectedUser;
            }
            set
            {
                selectedUser = value;
                RaisePropertyChanged("SelectedUser");
            }
        }

        public ItemClassification SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RaisePropertyChanged("SelectedItem");
            }
        }

        public Position SelectedPosition
        {
            get
            {
                return selectedPosition;
            }
            set
            {
                selectedPosition = value;
                RaisePropertyChanged("SelectedPosition");
            }
        }

        public ObservableCollection<User> Users
        {
            get;
            set;
        }

        public ObservableCollection<ItemClassification> Items
        {
            get;
            set;
        }

        public ObservableCollection<Position> Positions
        {
            get;
            set;
        }

        public ICommand CreateItemCommand
        {
            get
            {
                return createItemCommand;
            }
        }

        public ICommand CreateUserCommand
        {
            get
            {
                return createUserCommand;
            }
        }

        public ICommand CreatePositionCommand
        {
            get
            {
                return createPositionCommand;
            }
        }

        private void CreateItemExecute()
        {
            Messenger.Default.Send(new OpenCreateItemClassificationViewMessage());
        }

        private void CreateUserExecute()
        {
            Messenger.Default.Send(new OpenCreateUserViewMessage());
        }

        private void CreatePositionExecute()
        {
            Messenger.Default.Send(new OpenCreatePositionViewMessage());
        }
    }
}
